<?php


namespace app\voip\controller;


use app\voip\func\QmFunc;
use app\voip\func\SeatsFunc;
use function Psy\info;
use qimo\Moor;
use think\Request;
use vae\controller\AdminCheckAuth;

class QimoController extends AdminCheckAuth
{
    public function login()
    {
        $result=checkSeats(vae_get_login_admin('id'));

        if ($result){
            $loginName=$result['accountid'];
            $this->redirect('voip/qimo/main',['loginName'=>$loginName,'password'=>$result['secret'],'loginType'=>'sip','agentStatus'=>'0']);
        }
        return view();
    }
    public function main()
    {
        return view();
    }
    public function callOut()
    {
        try{
            $seatsInfo=$this->getSeatsBindInfo(vae_get_login_admin('id'));
            $number=$seatsInfo['number'];
            $phone=session('callNumber');
            $moor=new Moor();
            switch (session('callType')){
                case 1:
                    $result=$moor->setsMixPost($number,$phone,'gateway');
                    break;
                case 2:
                    $result=$moor->setsMixPost($number,$phone,'Local');
                    break;
                default:
                    $result=$moor->seatsPost($number,$phone,'sip');
                    break;
            }
            if (!$result['Succeed']){
                throw new \Exception($result['Message']);
            }
            return ['status'=>1,'msg'=>'拨出成功！','ActionID'=>$result['ActionID']];

        }catch (\Exception $e){
            return ['status'=>0,'msg'=>$e->getMessage()];
        }
    }
    private function getSeatsBindInfo($id)
    {

        if (empty(session('seatsInfo'))){
            $seatsInfo=SeatsFunc::getUserSeatsInfo($id);
        }else{
            $seatsInfo=session('seatsInfo');
        }
        return $seatsInfo;
    }


    /**
     * @return array
     */
    public function callEnd()
    {
        try{
            $seatsInfo=$this->getSeatsBindInfo(vae_get_login_admin('id'));
            $number=$seatsInfo['number'];
            $callId=input('uniqid');
            $moor=new Moor();
            $result=$moor->callEndData($callId,$number);
            if (!$result['Succeed']){
                throw new \Exception($result['Message']);
            }
            return ['status'=>1,'msg'=>'挂断成功！'];
        }catch (\Exception $e){
            return ['status'=>0,'msg'=>'挂断失败了！'];
        }
    }


    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View
     */
    public function seats()
    {
        return view();
    }

    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View
     */
    public function seatsAdd()
    {
        return view();
    }

    /**
     *
     */
    public function seatsAddData()
    {
        try{
            SeatsFunc::add(input());
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }
    /**
     *
     */
    public function seatsAddBindData()
    {
        try{
            SeatsFunc::bindAdd(input());
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }

    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View
     */
    public function seatsBind()
    {
        return view();
    }
    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View
     */
    public function seatsBindAdd()
    {
        return view();
    }
    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View|void
     * @throws \think\exception\DbException
     */
    public function ajaxGetSeatsList()
    {
        if (!request()->isAjax()){return;}
        $where = array();
        $param = vae_get_param();

        if(!empty($param['keywords'])) {
            $where['id|number'] = ['like', '%' . $param['keywords'] . '%'];
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];

        return vae_assign_table(0,'',SeatsFunc::getList($where,$rows,$param));
    }
    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View|void
     * @throws \think\exception\DbException
     */
    public function ajaxGetSeatsBindList()
    {
        if (!request()->isAjax()){return;}
        $where = array();
        $param = vae_get_param();

        if(!empty($param['keywords'])) {
            $where['s1.id|s2.number|a1.username'] = ['like', '%' . $param['keywords'] . '%'];
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];
        return vae_assign_table(0,'',SeatsFunc::getBindList($where,$rows,$param));
    }

    public function delBind($id)
    {
        try{

            SeatsFunc::delBind($id);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }

    /**
     * @param $id
     * @param $status
     */
    public function updateStatusBind($id, $status)
    {
        try{

            SeatsFunc::updateBind(['id'=>$id,'status'=>$status]);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }

    /**
     * @return \Swoft\Http\Message\Server\Response|\think\response\View
     */
    public function userSig()
    {
        return view();
    }

    /**
     * @throws \think\exception\DbException
     */
    public function ajaxGetUserSig()
    {
        if (!request()->isAjax()){return ;}

        $where = array();
        $param = vae_get_param();
        if(!empty($param['keywords'])) {
            $data=[];
            $data['signame']=$param['keywords'];
            try{
                QmFunc::userSigAdd($data);
            }catch (\Exception $e){

            }
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];
        return vae_assign_table(0,'',QmFunc::getSigList($where,$rows,$param));
    }


    /**
     * @param $id
     */
    public function delUserSig($id)
    {
        try{
            QmFunc::delUserSig($id);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }
    /**
     * @param $id
     */
    public function delSeats($id)
    {
        try{
            QmFunc::delSeats($id);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }
    public function excelType()
    {
        return view();
    }
    /**
     * @throws \think\exception\DbException
     */
    public function ajaxGetExcelType()
    {
        if (!request()->isAjax()){return ;}

        $where = array();
        $param = vae_get_param();
        if(!empty($param['keywords'])) {
            $data=[];
            $data['name']=$param['keywords'];
            try{
                QmFunc::excelTypeAdd($data);
            }catch (\Exception $e){

            }
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];
        return vae_assign_table(0,'',QmFunc::getExcelTypeList($where,$rows,$param));
    }
    /**
     * @param $id
     */
    public function delExcelType($id)
    {
        try{
            QmFunc::delExcelType($id);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }
}
